﻿using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using pinduoduo.Model;
using Sunny.UI;
using Tk.Soc.License.Utils;

namespace pinduoduo
{
    public partial class RegisterUser : UIForm
    {
        private string uuid;
        private string machineCode;
        public RegisterUser()
        {
            InitializeComponent();
        }

        private async void RegisterUser_Load(object sender, EventArgs e)
        {
            await CaptchaImage();
            this.machineCode = HardwareInfoHelper.GetWinHardwareInfoCode();
        }

        private async Task CaptchaImage()
        {
            var response = await G_Api.CaptchaImage();
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var jo = JObject.Parse(response.Content);

                if (jo["code"].ToString() == "200")
                {
                    uuid = jo["uuid"].ToString();
                    if (!string.IsNullOrWhiteSpace(jo["img"].ToString()))
                    {
                        byte[] bytes = Convert.FromBase64String(jo["img"].ToString());
                        MemoryStream s = new MemoryStream(bytes, true);
                        s.Write(bytes, 0, bytes.Length);

                        Image a = new Bitmap(s);

                        Bitmap bit = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
                        Graphics g = Graphics.FromImage(bit);//从指定的 Image 创建新的 Graphics(绘图)。
                        g.DrawImage(a, new Rectangle(0, 0, bit.Width, bit.Height), new Rectangle(0, 0, a.Width, a.Height), GraphicsUnit.Pixel);
                        this.pictureBox1.Image = bit;
                    }
                }
                else
                {
                    this.msg.Text = "获取验证码失败";
                }
            }
        }

        private async void pictureBox1_Click(object sender, EventArgs e)
        {
           await CaptchaImage();
        }

        private async void  uiButton2_Click(object sender, EventArgs e)
        {
            string userName = this.userName.Text.Trim();
            string password = this.password.Text.Trim();
            string newPassword = this.newPassword.Text.Trim();
            string phonenumber = this.phonenumber.Text.Trim();
            string code = this.code.Text;
            this.msg.Text = "";
            if (userName.Length < 5 || userName.Length>10)
            {
                this.msg.Text = "用户名长度在5-10位";
                return;
            }

            if (password.Length<5 || password.Length > 10)
            {
                this.msg.Text = "密码长度在5-10位";
                return;
            }

            if (!IsHandset(phonenumber))
            {
                this.msg.Text = "手机号格式不正确";
                return;
            }

            if (password != newPassword)
            {
                this.msg.Text = "俩次输入的密码不正确";
                return;
            }

            if (string.IsNullOrWhiteSpace(code))
            {
                this.msg.Text = "验证码不能为空";
                return;
            }
            ApiUser apiUser = new ApiUser();
            apiUser.code = code;
            apiUser.uuid = uuid;
            apiUser.userName = userName;
            apiUser.nickName = userName;
            apiUser.phonenumber = phonenumber;
            apiUser.password = password;
            var res = await  G_Api.RegisterUser(apiUser, machineCode,code,uuid);
            if (res.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var jo = JObject.Parse(res.Content);

                if (jo["code"].ToString() == "200")
                {
                    this.msg.Text = "注册成功";
                }
                else 
                {
                    this.msg.Text = jo["msg"].ToString();
                }
            }
            else 
            {
                this.msg.Text = "注册异常";
            }
            await CaptchaImage();
        }


        public bool IsTelephone(string telephone)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(telephone, @"^(\d{3,4}-)?\d{6,8}$");
        }

        public bool IsHandset(string str_handset)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5]+\d{9}");
        }
    }


}
